﻿Imports Lourdes.BackEnd.BC
Imports Lourdes.BackEnd.Modelo
Imports Lourdes.BackEnd
Imports System.Configuration
Public Class ucObraSocialSelector
  Public Event ListaPrecioSeleccionada(ByVal pListaDePrecioId As Integer?)

  Private Sub EstablecerEstadoControles()
    If chkUsa.Checked Then
      lblObraSocial.Enabled = True
      lblPlan.Enabled = True
      cmbOS.Enabled = True
      cmbPlan.Enabled = True
    Else
      lblObraSocial.Enabled = False
      lblPlan.Enabled = False
      cmbOS.Enabled = False
      cmbPlan.Enabled = False
    End If
  End Sub

  Private Sub chkUsa_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkUsa.CheckedChanged
    EstablecerEstadoControles()
    RaiseEvent ListaPrecioSeleccionada(getListaPrecioSeleccionada())
  End Sub

  Public Sub CargarCliente(ByVal pPerId As Integer?, Optional pPlanId As Integer? = Nothing)
    Dim objBcPer As BC_Persona
    Dim lstOs As List(Of Persona)
    Try
      objBcPer = New BC_Persona

      If Not pPerId.HasValue Then
        With chkUsa
          .Checked = False
          .Enabled = False
        End With
        With cmbOS
          .DataSource = Nothing
          .Items.Clear()
          .Enabled = False
        End With
        With cmbPlan
          .DataSource = Nothing
          .Items.Clear()
          .Enabled = False
        End With

      Else
        lstOs = objBcPer.getObraSocialbyPersonaId(pPerId)
        With cmbOS
          .DataSource = lstOs
          .DisplayMember = "Nombre"
          .ValueMember = "PersonaId"
        End With
        If lstOs.Count > 0 Then
          cmbOS.SelectedIndex = 0
          With chkUsa
            '  .Checked = True
            .Enabled = True
          End With
        Else
          With chkUsa
            .Checked = False
            .Enabled = False
          End With
        End If
        CargarPlanesCliente()

        If pPlanId.HasValue Then
          SetSelectedPlanObraSocial(pPlanId)
        End If

      End If

      RaiseEvent ListaPrecioSeleccionada(getListaPrecioSeleccionada())

    Catch ex As Exception
      Throw ex
    End Try
  End Sub

  Private Sub SetSelectedPlanObraSocial(pPlanId As Integer)
    Dim objBcPlan As BC_PlanMedico
    Dim objPlan As PlanMedico
    objBcPlan = New BC_PlanMedico

    objPlan = objBcPlan.GetByPK(pPlanId).FirstOrDefault
    With objPlan   
      cmbOS.SelectedValue = .ObraSocialPerId
      cmbPlan.SelectedValue = .PlanMedicoId
    End With
    chkUsa.Checked = True
  End Sub

  Private Sub CargarPlanesCliente()
    Dim objBcPm As BC_PlanMedico
    Dim lstPm As List(Of PlanMedico)
    Try
      lstPm = New List(Of PlanMedico)
      objBcPm = New BC_PlanMedico
      If cmbOS.Items.Count > 0 Then
        lstPm = objBcPm.GetByFilter(New PlanMedicoFilter With {.ObraSocialPerId = cmbOS.SelectedValue})
      End If
      With cmbPlan
        .DataSource = lstPm
        .DisplayMember = "Nombre"
        .ValueMember = "PlanMedicoId"
      End With

    Catch ex As Exception
      Throw ex
    End Try

  End Sub

  Private Sub ucObraSocialSelector_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    EstablecerEstadoControles()
  End Sub

  Private Sub cmbOS_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbOS.SelectionChangeCommitted
    CargarPlanesCliente()
    RaiseEvent ListaPrecioSeleccionada(getListaPrecioSeleccionada())
  End Sub

  Private Sub cmbPlan_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles cmbPlan.SelectionChangeCommitted
    RaiseEvent ListaPrecioSeleccionada(getListaPrecioSeleccionada())
  End Sub

  Public Function getOsSeleccionada() As Nullable(Of Integer)
    If chkUsa.Checked Then
      If IsNumeric(cmbOS.SelectedValue) Then
        Return CInt(cmbOS.SelectedValue)
      End If
    End If
    Return Nothing
  End Function

  Public Function getPlanSeleccionado() As Nullable(Of Integer)
    If chkUsa.Checked Then
      If IsNumeric(cmbPlan.SelectedValue) Then
        Return CInt(cmbPlan.SelectedValue)
      End If
    End If
    Return Nothing
  End Function

  Public Function getListaPrecioSeleccionada() As Nullable(Of Integer)
    If chkUsa.Checked Then
      Dim objPlan As PlanMedico
      objPlan = TryCast(cmbPlan.Items(cmbPlan.SelectedIndex), PlanMedico)
      If objPlan IsNot Nothing Then
        Return objPlan.ListaPrecioId
      Else
        Return Nothing
      End If
    End If
    Return Nothing
  End Function


End Class
